package main

import (
	"github.com/go-gormigrate/gormigrate/v2"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"learn_login/model"
	"log"
)

var db *gorm.DB

func GetDB() *gorm.DB {
	return db
}

func initDB() {
	var err error
	db, err = gorm.Open(sqlite.Open("local.db"), &gorm.Config{})
	if err != nil {
		log.Panicln(err)
	}

	m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{})

	m.InitSchema(func(db *gorm.DB) error {
		err = db.AutoMigrate(
			// 初始化模型
			&model.User{},
		)
		if err != nil {
			return err
		}
		return nil
	})

	err = m.Migrate()
	if err != nil {
		log.Panicln(err)
	}
	log.Println("数据迁移成功")
}
